package com.customer.crm.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.customer.crm.system.pojo.SystemFunction;
import com.customer.crm.system.pojo.SystemRole;
import com.customer.crm.system.pojo.SystemUser;
import com.customer.crm.system.service.SystemFunctionService;
import com.customer.crm.system.service.SystemRoleService;
import com.customer.crm.system.service.SystemUserService;
import com.customer.crm.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * The type Login controller.
 *
 * @author Lyp
 * @create 2021 /12/15
 */
@Controller
public class LoginController extends BaseController {

    /**
     * The System user service.
     */
    @Autowired
    SystemUserService systemUserService;

    /**
     * The System role service.
     */
    @Autowired
    SystemRoleService systemRoleService;

    /**
     * The System function service.
     */
    @Autowired
    SystemFunctionService systemFunctionService;

    /**
     * Index string.
     * 通过 用户名  密码查询  将用户信息放到session 中
     *
     * @param userName the user name
     * @param password the password
     * @param model    the model
     * @param session  the session
     * @param response the response
     * @return the string
     * @throws IOException the io exception
     */
    @RequestMapping("/index")
    public String index(@RequestParam("username") String userName,
                        @RequestParam("password") String password,
                        Model model, HttpSession session,
                        HttpServletResponse response) throws IOException {

        QueryWrapper<SystemUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", userName).eq("password", password);
        SystemUser systemUser = systemUserService.getOne(wrapper);

        if (systemUser != null) {
            List<SystemFunction> userFunc = systemFunctionService.findByRoleId(systemUser.getId());
            List<String> func = new ArrayList<>();
            for (SystemFunction systemFunction : userFunc) {
                func.add(systemFunction.getFuncURL());
            }
            session.setAttribute("systemUser", systemUser);
            session.setAttribute("func", func);
            resultData = ResultData.buildSuccessResult("登录成功");
            return "views/index";
        } else {
            model.addAttribute("msg", "账号或者密码不正确");
            return "login";
        }
    }


    /**
     * To user update string.
     * 修改资料   跳转修改页面
     *
     * @param session the session
     * @param model   the model
     * @return the string
     */
    @GetMapping("/toUserUpdate")
    public String toUserUpdate(HttpSession session, Model model) {
        SystemUser user = (SystemUser) session.getAttribute("systemUser");
        QueryWrapper<SystemUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", user.getUserName());
        SystemUser systemUser = systemUserService.getOne(wrapper);
        model.addAttribute("userVO", systemUser);
        return "views/system/edit_user";
    }

    /**
     * User update string
     * 修改用户信息
     *
     * @param systemUser the system user
     * @return the string
     */
    @RequestMapping("/userUpdate")
    @ResponseBody
    public ResultData userUpdate(SystemUser systemUser, ModelMap modelMap) {
        boolean b = systemUserService.updateById(systemUser);
        modelMap.put("success" , b);

        ResultData resultData = ResultData.buildSuccessResult(null, b);

        return resultData;
    }

    @GetMapping("/logOut")
    public String logOut(HttpSession session) {
        session.invalidate();
        return "login";
    }
}
